<!doctype html>
<html>

	<head>
		<meta charset="UTF-8">
		<title></title>
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<link href="../css/mui.min.css" rel="stylesheet" />
		<link rel="stylesheet" href="../css/icons-extra.css" />
	</head>

	<body>
		<header class="mui-bar mui-bar-nav">	
			<a class="mui-icon mui-icon-back mui-action-back mui-pull-left"></a>
			<a class="mui-icon-extra mui-icon-extra-topic mui-pull-right" style="margin-top: 10px;"
				 onclick="scanPicture()"></a>
		</header>
		<div class="mui-content" id="divContent" style="width: 100%; background-color: black;top:0px;bottom: 0px;position: absolute;">
		</div>
		<script src="../js/mui.min.js"></script>
		<script type="text/javascript">
			mui.init();
			
			var self=null,opener=null;
			var scan=null,domready=false;
			// H5 plus事件处理
			function plusReady(){
				if(self||!window.plus||!domready){
					return;
				}
				// 获取窗口对象
				self=plus.webview.currentWebview();
				opener=self.opener();
				// 开始扫描
				self.addEventListener('show', function(){
					//scan=new plus.barcode.Barcode('divContent');
					scan = new plus.barcode.Barcode("divContent",[plus.barcode.QR,plus.barcode.EAN8,plus.barcode.EAN13],{frameColor:'#00FF00',scanbarColor:'#00FF00'});
				    scan.onmarked=onmarked;
				    scan.start({conserve:true,filename:'_doc/barcode/'});
				}, false);
				// 显示页面并关闭等待框
			    self.show('pop-in');
			    opener.evalJS('closeWaiting()');
			}
			
			if(window.plus){
				plusReady();
			}else{
				document.addEventListener('plusready', plusReady, false);
			}
			// 监听DOMContentLoaded事件
			document.addEventListener('DOMContentLoaded', function(){
				domready=true;
				plusReady();
			}, false);
			
			// 二维码扫描成功
			function onmarked(type, result, file){
			    switch(type){
			    	case plus.barcode.QR:
			    	type = 'QR';
			    	break;
			    	case plus.barcode.EAN13:
			    	type = 'EAN13';
			    	break;
			    	case plus.barcode.EAN8:
			    	type = 'EAN8';
			    	break;
			    	default:
			    	type = '其它'+type;
			    	break;
			    }
			    result = result.replace(/\n/g, '');
			    opener.evalJS("scaned('"+ type +"','"+ result +"','"+ file +"');");
			    self.close();
			}
			// 从相册中选择二维码图片 
			function scanPicture(){
			    plus.gallery.pick(function(path){
				    plus.barcode.scan(path,onmarked,function(error){
				    	console.log(error);
						plus.nativeUI.alert('无法识别此图片');
					});
			    }, function(err){
			        console.log('Failed: '+err.message);
			    });
			}
		</script>
	</body>

</html>